
export function timeFix () {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}

export function welcome () {
  const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 DOTA', '我猜你可能累了']
  const index = Math.floor(Math.random() * arr.length)
  return arr[index]
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent () {
  const event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

export function handleScrollHeader (callback) {
  let timer = 0

  let beforeScrollTop = window.pageYOffset
  callback = callback || function () {}
  window.addEventListener(
    'scroll',
    event => {
      clearTimeout(timer)
      timer = setTimeout(() => {
        let direction = 'up'
        const afterScrollTop = window.pageYOffset
        const delta = afterScrollTop - beforeScrollTop
        if (delta === 0) {
          return false
        }
        direction = delta > 0 ? 'down' : 'up'
        callback(direction)
        beforeScrollTop = afterScrollTop
      }, 50)
    },
    false
  )
}

export function isIE () {
  const bw = window.navigator.userAgent
  const compare = (s) => bw.indexOf(s) >= 0
  const ie11 = (() => 'ActiveXObject' in window)()
  return compare('MSIE') || ie11
}

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
export function removeLoadingAnimate (id = '', timeout = 1500) {
  if (id === '') {
    return
  }
  setTimeout(() => {
    document.body.removeChild(document.getElementById(id))
  }, timeout)
}

/**
 * 存储至sessionStorage
 * @param name
 * @param data
 */
export function setItemToSession (name, data) {
  sessionStorage.setItem(name, JSON.stringify(data))
}

/**
 * 从sessionStorage获取数据
 * @param name
 */
export function getItemFromSession (name) {
  return JSON.parse(sessionStorage.getItem(name))
}

/**
 * 组装gis地址
 * @param params 参数
 * @param baseUrl config.js中配置的地址
 * baseUrl=http://192.168.2.28:7091/ywmap/?system=zrzy_dtsp#/
 */
export function assembleGisUrl (baseUrl, params) {
  let queryParams = {}
  if (baseUrl && baseUrl.indexOf('#/') !== -1 && baseUrl.indexOf('?') !== -1) {
    var query = baseUrl.split('?')[1].split('#/')[0]
    queryParams = GetParam(query)
    queryParams = Object.assign(params, queryParams)
    var str = buildStr(queryParams)
    baseUrl = baseUrl.split('?')[0] + '?' + str + '#/' + baseUrl.split('#/')[1]
  } else if (baseUrl.indexOf('?') !== -1) {
    const str = buildStr(params)
    baseUrl = baseUrl.split('?')[0] + '?' + str
  }
  return baseUrl
}

function buildStr (queryParams) {
  let str = ''
  let temp = ''
  for (var key in queryParams) {
    // eslint-disable-next-line no-prototype-builtins
    if (queryParams.hasOwnProperty(key)) {
      str += temp + key + '=' + queryParams[key]
      temp = '&'
    }
  }
  return str
}

function GetParam (strs) {
  const qp = {}
  strs = strs.split('&')
  for (var i = 0; i < strs.length; i++) {
    var sa = strs[i].split('=')
    qp[sa[0]] = sa.length === 1 ? '' : unescape(sa[1])
  }
  return qp
}

export function sleep (time) {
  return new Promise(resolve => {
    setTimeout(resolve, time)
  })
}
